package cuiyt.juc.unsafe;


import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author cyt
 * @create 2020-05-01 15:32
 */
// Exception in thread "43" java.util.ConcurrentModificationException
public class SetTest {
    public static void main(String[] args) {
        Set<String> set = new HashSet<String>();
/**
 * 1、Collections.synchronizedSet(set);
 * 2、new CopyOnWriteArraySet<>();
 */
        Set<String> set1 = Collections.synchronizedSet(set);
        CopyOnWriteArraySet<Object> set2 = new CopyOnWriteArraySet<>();
        for (int i = 0; i <
                50; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0, 5));
                System.out.println(set.toString());
            }, String.valueOf(i)).start();
        }
    }
}
